//
//  SettingViewController.m
//  Gunz
//
//  Created by Joggy on 15/12/12.
//  Copyright © 2015年 NUPT. All rights reserved.
//

#import "SettingViewController.h"
#import "AppDelegate.h"
#import "Gunz-Swift.h"
#import "QYNotice.h"
#import "SInstructionsViewController.h"
#import "SFeedbackViewController.h"
#import "SAboutusViewController.h"
#import "SShareappViewController.h"
#import "SMoreViewController.h"
#import "STableViewCell1.h"
#import "STableViewCell2.h"

@implementation SettingViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self prepareForNav];
    [self prepareForLayout];
    icons = @[
              @[@"01_liuliangjiesheng", @"02_qingkonghuancun", @"03_jianchagengxin", @"04_shiyongshuoming"],
              @[@"05_yingyongfankui", @"06_guanyuwomen", @"07_fenxiangyingyong", @"08_gengduo"],
              ];
    titles = @[
               @[@"流量节省", @"清空缓存", @"检查更新", @"使用说明"],
               @[@"应用反馈", @"关于我们", @"分享应用", @"更多"],
               @[@"退出"]
               ];
}

-(void)prepareForNav{
    [super prepareForNav];
    self.title = @"设置";
}

-(void)prepareForLayout {
    [super prepareForLayout];
    tb = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    tb.delegate = self;
    tb.dataSource = self;
    tb.backgroundColor = [UIColor clearColor];
    tb.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 10)];
    tb.separatorStyle = UITableViewCellSelectionStyleBlue;
    tb.separatorColor = [UIColor colorWithRed:0.85 green:0.85 blue:0.85 alpha:1];
    tb.showsVerticalScrollIndicator = false;
    [tb registerClass:[STableViewCell1 class] forCellReuseIdentifier:@"SCell1"];
    [tb registerClass:[STableViewCell2 class] forCellReuseIdentifier:@"SCell2"];
    [self.view addSubview:tb];
}

#pragma 事件方法

-(void)exitApp {
    UIAlertView *quitAlert = [[UIAlertView alloc] initWithTitle:@"确认退出?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [quitAlert show];
}

- (void)pushToInstructions {
    SInstructionsViewController *instructionsVC = [[SInstructionsViewController alloc] init];
    instructionsVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:instructionsVC animated:true];
}

- (void)pushToFeedback {
    SFeedbackViewController *feedbackVC = [[SFeedbackViewController alloc] init];
    feedbackVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:feedbackVC animated:true];
}

- (void)pushToAboutus {
    SAboutusViewController *aboutusVC = [[SAboutusViewController alloc] init];
    aboutusVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:aboutusVC animated:true];
}

- (void)pushToShareapp {
    SShareappViewController *shareappVC = [[SShareappViewController alloc] init];
    shareappVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:shareappVC animated:true];
}

- (void)pushToMore {
    SMoreViewController *moreVC = [[SMoreViewController alloc] init];
    moreVC.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:moreVC animated:true];
}

#pragma 代理方法

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:true];
    if(indexPath.section < 2) {
        if(indexPath.section == 0) {
            switch (indexPath.row) {
                case 0:
                    break;
                case 1:
                    [QYNotice showSuccess:@"清理缓存成功" toView:self.view];
                    [QYNotice dismissWithDelay:3];
                    break;
                case 2:
                    [QYNotice showWarning:@"暂无更新" toView:self.view];
                    [QYNotice dismissWithDelay:3];
                    break;
                case 3:
                    [self pushToInstructions];
                    break;
                default:
                    break;
            }
        }
        if(indexPath.section == 1) {
            switch (indexPath.row) {
                case 0:
                    [self pushToFeedback];
                    break;
                case 1:
                    [self pushToAboutus];
                    break;
                case 2:
                    [self pushToShareapp];
                    break;
                case 3:
                    [self pushToMore];
                    break;
                default:
                    break;
            }
        }
    }
    else {
        [self exitApp];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 47;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 3;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *arr = titles[section];
    return arr.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 10)];
    v.backgroundColor = [UIColor clearColor];
    return v;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0 && indexPath.row == 0) {
        STableViewCell1 *cell = [tableView dequeueReusableCellWithIdentifier:@"SCell1"];
        NSArray *arr = titles[indexPath.section];
        cell.textLabel.text = arr[indexPath.row];
        NSArray *arr1 = icons[indexPath.section];
        cell.imageView.image = [UIImage imageNamed:arr1[indexPath.row]];
        cell.backgroundColor = [UIColor colorWithRed:0.9765 green:0.9765 blue:0.9843 alpha:1];
        cell.textLabel.textColor = [UIColor colorWithRed:0.298 green:0.345 blue:0.4235 alpha:1];
        return cell;
    }
    else {
        STableViewCell2 *cell = [tableView dequeueReusableCellWithIdentifier:@"SCell2"];
        if(indexPath.section < 2) {
            NSArray *arr = titles[indexPath.section];
            cell.textLabel.text = arr[indexPath.row];
            NSArray *arr1 = icons[indexPath.section];
            cell.imageView.image = [UIImage imageNamed:arr1[indexPath.row]];
        }
        else {
            cell.textLabel.text = @"退出登录";
            cell.textLabel.textAlignment = NSTextAlignmentCenter;
            cell.arrow.alpha = 0;
        }
        cell.backgroundColor = [UIColor colorWithRed:0.9765 green:0.9765 blue:0.9843 alpha:1];
        cell.textLabel.textColor = [UIColor colorWithRed:0.298 green:0.345 blue:0.4235 alpha:1];
        return cell;
    }
}

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if(buttonIndex == 1) {
        AppDelegate *app = [[UIApplication sharedApplication] delegate];
        app.window.rootViewController = app.lv;
    }
}

@end
